<!--
  This file is a part of the open-eBackup project.
  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
  If a copy of the MPL was not distributed with this file, You can obtain one at
  http://mozilla.org/MPL/2.0/.
  
  Copyright (c) [2024] Huawei Technologies Co.,Ltd.
  
  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  -->

<ng-container *ngIf="!fileLevelRestore">
    <ng-container *ngIf="selectedPathData.length > 1; else selectTitleTpl">
        <lv-collapse [lvMultiExpansion]="'false'" lvType="simple">
            <lv-collapse-panel [lvTitle]="selectTitleTpl" [lvExpanded]="false">
                <lv-datatable [lvData]='selectedPathData' #lvTable [lvPaginator]='page' lvSize="small">
                    <thead>
                        <tr>
                            <th lvCellKey='name'>{{'common_name_label' | i18n}}</th>
                        </tr>
                    </thead>
                    <tbody>
                        <ng-container *ngFor='let item of lvTable.renderData'>
                            <tr>
                                <td>
                                    <span lv-overflow>
                                        {{item.name}}
                                    </span>
                                </td>
                            </tr>
                        </ng-container>
                    </tbody>
                </lv-datatable>
                <lv-paginator #page lvMode="simple" [lvShowPageSizeOptions]="false" [hidden]="!selectedPathData?.length"
                    [lvPageSize]="pageSize">
                </lv-paginator>
            </lv-collapse-panel>
        </lv-collapse>
    </ng-container>
    <ng-template #selectTitleTpl>
        <lv-group [lvGutter]="'14px'">
            <span class="selected-disk-title">{{'protection_select_path_label' | i18n }}</span>
            <div style="width: max-600px;" lv-overflow>
                <span> {{selectedPathData.length > 1 ? selectedPathData.length :
                    selectedPathData.length ? selectedPathData[0].name :''}}</span>
            </div>
        </lv-group>
    </ng-template>
</ng-container>


<lv-form [formGroup]="formGroup" [lvLabelColon]="false" class="formGroup">
    <lv-form-item>
        <lv-form-label lvRequired>{{'protection_restore_to_label' | i18n}}</lv-form-label>
        <lv-form-control>
            <lv-radio-group formControlName="restoreLocation">
                <lv-group [lvGutter]="'30px'">
                    <lv-radio
                        [lv-tooltip]="disableOriginLocation?('protection_origin_file_restore_disabled_label'| i18n):''"
                        [lvDisabled]="disableOriginLocation" [lvValue]="restoreLocationType.ORIGIN">
                        {{'common_restore_to_origin_location_label' |
                        i18n}}</lv-radio>
                    <lv-radio [lvValue]="restoreLocationType.NEW">{{'common_restore_to_new_location_label' | i18n}}
                    </lv-radio>
                </lv-group>
            </lv-radio-group>
        </lv-form-control>
    </lv-form-item>

    <ng-container *ngIf="formGroup.value.restoreLocation === restoreLocationType.ORIGIN">
        <lv-form-item>
            <lv-form-label>{{'protection_computer_location_label' | i18n}}</lv-form-label>
            <lv-form-control>
                <span>{{formGroup.value.location | nil}}</span>
            </lv-form-control>
        </lv-form-item>
        <lv-form-item>
            <lv-form-label lvRequired>
                {{'protection_restore_vm_ip_label' | i18n}}
                <i lv-icon="aui-icon-help" lvTooltipTheme="light" lvTooltipPosition="bottom"
                    lv-tooltip="{{ 'protection_file_restore_ip_help_label' | i18n: [rowCopy.resource_environment_name] }}"
                    class="configform-constraint" lvColorState='true'></i>
            </lv-form-label>
            <lv-form-control>
                <ng-container *ngTemplateOutlet="vmIpTpl"></ng-container>
            </lv-form-control>
        </lv-form-item>
    </ng-container>
    <lv-form-item
        [ngClass]="formGroup.value.restoreLocation === restoreLocationType.NEW?'computer-location-show':'computer-location-hidden'">
        <lv-form-label>{{'protection_computer_location_label' | i18n}}</lv-form-label>
        <lv-form-control>
            <div [ngClass]="{'new-tip':true,'vm-location-placeHolder':!formGroup.value.location}" lv-overflow>
                {{formGroup.value.location
                || ('protection_computer_location_disk_tip_label' | i18n)}}
            </div>
            <aui-computer-location [vmRestoreOptionType]="VmRestoreOptionType.DISK"
                (changeLocation)='changeLocation($event)' [type]="RestoreType.FileRestore"
                (changeVcenter)="changeVcenter($event)"></aui-computer-location>
        </lv-form-control>
    </lv-form-item>
    <ng-container *ngIf="formGroup.value.restoreLocation === restoreLocationType.NEW">
        <lv-form-item>
            <lv-form-label>
                {{'protection_specified_path_label' | i18n}}
                <i lv-icon="aui-icon-help" lvTooltipTheme="light" lvTooltipPosition="bottom" [lv-tooltip]="helpTpl"
                    class="configform-constraint" lvColorState='true'></i>
            </lv-form-label>
            <lv-form-control [lvErrorTip]="pathErrorTip">
                <input lv-input type="text" formControlName="targetPath" autocomplete="new-password"
                    placeholder="{{'protection_specified_path_placeholder_label' | i18n}}" />
            </lv-form-control>
        </lv-form-item>
        <lv-form-item>
            <lv-form-label lvRequired>
                {{'protection_restore_vm_ip_label' | i18n}}
                <i lv-icon="aui-icon-help" lvTooltipTheme="light" lvTooltipPosition="bottom"
                    lv-tooltip="{{vmIpHelpLabel}}" class="configform-constraint" lvColorState='true'></i>
            </lv-form-label>
            <lv-form-control>
                <ng-container *ngTemplateOutlet="vmIpTpl"></ng-container>
            </lv-form-control>
        </lv-form-item>
    </ng-container>
    <!-- 新增端口 -->
    <lv-form-item *ngIf="showPort()">
        <lv-form-label lvRequired>
            {{ 'common_port_label' | i18n }}
        </lv-form-label>
        <lv-form-control [lvErrorTip]="portErrorTip">
            <input lv-input type="text" formControlName="port" placeholder="1~65535" />
        </lv-form-control>
    </lv-form-item>
    <lv-form-item>
        <lv-form-label lvRequired>{{'protection_restore_vm_user_label' | i18n}}</lv-form-label>
        <lv-form-control>
            <input lv-input type="text" formControlName="userName" autocomplete="new-password" />
        </lv-form-control>
    </lv-form-item>
    <lv-form-item>
        <lv-form-label lvRequired>{{'protection_restore_vm_password_label' | i18n}}</lv-form-label>
        <lv-form-control>
            <aui-inupt-with-eye formControlName="password" [lvPasteAllowed]="false"></aui-inupt-with-eye>
        </lv-form-control>
    </lv-form-item>
    <lv-form-item>
        <lv-form-label lvRequired>{{'protection_over_write_rule_label' | i18n}}</lv-form-label>
        <lv-form-control>
            <lv-radio-group formControlName="originalType" style="margin-top:8px;">
                <lv-group [lvGutter]="'20px'" lvDirection='vertical' class="mgt-8">
                    <lv-radio [lvValue]="vmFileReplaceStrategy.Overwriting">{{'protection_overwrite_label' | i18n}}
                    </lv-radio>
                    <lv-radio [lvValue]="vmFileReplaceStrategy.Skip">{{'protection_skip_label' | i18n}}</lv-radio>
                    <lv-radio [lvValue]="vmFileReplaceStrategy.Replace">
                        {{'protection_restore_replace_older_label' | i18n}}</lv-radio>
                </lv-group>
            </lv-radio-group>
        </lv-form-control>
    </lv-form-item>
</lv-form>

<ng-template #vmIpTpl>
    <ng-container [formGroup]="formGroup">
        <lv-radio-group formControlName="vmIpType">
            <lv-group [lvGutter]="'20px'">
                <lv-radio [lvValue]="vmIpTypeOptions.exist">
                    {{'protection_vm_restore_select_ip_label' | i18n}}
                </lv-radio>
                <lv-radio [lvValue]="vmIpTypeOptions.custom">
                    {{'protection_vm_restore_input_ip_label' | i18n}}
                </lv-radio>
            </lv-group>
        </lv-radio-group>
        <ng-container *ngIf="formGroup.value.vmIpType === vmIpTypeOptions.exist; else elseVmIpTemplate">
            <lv-form-control class="ip-control">
                <lv-select formControlName="vmIp" [lvOptions]='vmIpOptions' lvValueKey="key" [lvPlaceholder]="(vmIpNoData?'protection_restore_vm_ip_select_placeholder_label' :
                   'protection_restore_vm_ip_placeholder_label') | i18n" lvShowFilter lvFilterMode="contains"
                    lvFilterKey='label'>
                </lv-select>
            </lv-form-control>
        </ng-container>
        <ng-template #elseVmIpTemplate>
            <lv-form-control class="ip-control" [lvErrorTip]="ipErrorTip">
                <input lv-input type="text" formControlName="customIp" />
            </lv-form-control>
        </ng-template>
    </ng-container>
</ng-template>

<ng-template #footerTpl>
    <div class="lv-modal-btns">
        <lv-group lvGutter='8px'>
            <button lv-button lvType='primary' (click)='testConnection()' [disabled]='formGroup.invalid'
                [lvLoading]='testLoading'>
                {{ 'common_test_label' | i18n }}
            </button>
            <button lv-button lvType='primary' (click)='restore()' [disabled]='formGroup.invalid || !isTest'
                [lvLoading]='okLoading'>
                {{ 'common_ok_label' | i18n }}
            </button>
            <button lv-button (click)='modal.close()'>
                {{ 'common_cancel_label' | i18n }}
            </button>
        </lv-group>
    </div>
</ng-template>

<ng-template #helpTpl>
    <span innerHTML="{{'protection_specified_path_help_label' | i18n}}"></span>
</ng-template>